package com.act;

import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bean.Answer;
import com.bean.AnswerList;
import com.common.Constant;
import com.common.TextHttpResponseHandler;
import com.mUI.XListView;
import com.mUI.XListView.IXListViewListener;
import com.xike.kyt.R;
import com.xike.util.MyUtil;

public class Answer_ListAct extends BaseAct {
	TextView tv_back;
	XListView listView;
	MyAdapter adapter;
	int pageCursor = 1;
	boolean isRefresh = false;
	boolean isLoadingMore = false;
	AnswerList answerList;
	ArrayList<Answer> answers = new ArrayList<Answer>();
	long user_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_answer_list);

		Bundle bundle = getIntent().getExtras();
		if (bundle != null && bundle.containsKey("user_id")) {
			user_id = bundle.getLong("user_id");
		}

		initViews();
		serviceGetMyAnswer();
	}

	@Override
	protected void initViews() {
		tv_back = (TextView) findViewById(R.id.headTitle_tv_back);
		tv_back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		listView = (XListView) findViewById(R.id.myAnswer_listView);
		listView.setPullRefreshEnable(true);
		listView.setPullLoadEnable(true);
		listView.setAutoLoadEnable(true);

		adapter = new MyAdapter();
		listView.setAdapter(adapter);

		listView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				pageCursor = 1;
				isRefresh = true;
				serviceGetMyAnswer();
			}

			@Override
			public void onLoadMore() {
				pageCursor++;
				isLoadingMore = true;
				serviceGetMyAnswer();
			}
		});
	}

	@Override
	protected void updateViews() {
		ArrayList<Answer> loadAnswers = answerList.getAnswers();
		if (loadAnswers == null || loadAnswers.size() == 0) {
			if (pageCursor == 1) {
				findViewById(R.id.layout_nodata).setVisibility(View.VISIBLE);
				listView.setVisibility(View.GONE);
				((ImageView) findViewById(R.id.layout_nodata_img)).setImageResource(R.drawable.default_nodata_answer);
				((TextView) findViewById(R.id.layout_nodata_tv_tips)).setText("没有任何答案\n分享您的答案即是分享您的快乐");
			}

		} else {
			findViewById(R.id.layout_nodata).setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);

			if (isRefresh) {
				answers.clear(); // 刷新 清空
				answers.addAll(loadAnswers);
				adapter.notifyDataSetChanged();
				listView.stopRefresh();
				listView.updateRefreshTime();
				isRefresh = false;
				listView.setPullLoadEnable(true);
			} else {
				answers.addAll(loadAnswers);
				adapter.notifyDataSetChanged();
				if (isLoadingMore) {
					listView.stopLoadMore();
				}
				if (answerList.getNext_cursor() > 0) {
					listView.showFooterView();
				} else {
					listView.setPullLoadEnable(false);
				}
			}
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					Intent it_answerDetail = new Intent(Answer_ListAct.this, Answer_DetailAct.class);
					it_answerDetail.putExtra("answerId", id);
					startActivity(it_answerDetail);
				}

			});
		}
	}

	private void serviceGetMyAnswer() {
		ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("user_id", String.valueOf(user_id)));
		params.add(new BasicNameValuePair("page_cursor", String.valueOf(pageCursor)));
		params.add(new BasicNameValuePair("page_count", String.valueOf(Constant.LIST_PAGESIZE)));
		String strUrl = MyUtil.signRequestUrl(Constant.SERVER_URL_ANSWER_LIST, params);
		client.get(this, strUrl, new TextHttpResponseHandler() {

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				mToast.showToast(Answer_ListAct.this, getString(R.string.net_error));
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				AnswerList result = gson.fromJson(responseString, AnswerList.class);
				dealRequestResult(result);
			}

		});
	}

	private void dealRequestResult(AnswerList result) {
		if (result != null) {
			if (result.isReturnTrue()) {
				answerList = result;
				updateViews();
			} else {
				mToast.showToast(Answer_ListAct.this, result.getError_description());
			}
		} else {
			mToast.showToast(Answer_ListAct.this, getString(R.string.net_error));
		}
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return answers.size();
		}

		@Override
		public Answer getItem(int position) {
			return answers.get(position);
		}

		@Override
		public long getItemId(int position) {
			return getItem(position).getId();
		}

		class ViewHolder {
			TextView tv_content;
			TextView tv_questionTitle;
		}

		ViewHolder holder;

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = View.inflate(Answer_ListAct.this, R.layout.listitem_my_anwser, null);
				holder.tv_questionTitle = (TextView) convertView.findViewById(R.id.lvitem_myAnswer_tv_questionTitle);
				holder.tv_content = (TextView) convertView.findViewById(R.id.lvitem_myAnswer_tv_content);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			final Answer answer = getItem(position);

			MyUtil.setTextViewText(holder.tv_questionTitle, answer.getQuestion().getTitle(), View.GONE, "");
			MyUtil.setTextViewText(holder.tv_content, answer.getContent(), View.GONE, "");
			holder.tv_questionTitle.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					if (((TextView) v).getLineCount() == 1) {
						((TextView) v).setSingleLine(false);
						((TextView) v).setText(answer.getQuestion().getTitle());
					} else {
						((TextView) v).setSingleLine(true);
						((TextView) v).setText(answer.getQuestion().getTitle());
					}
				}
			});
			return convertView;
		}

	}

}
